Skip to content

Method: createQuery(String, Class)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.sparql;
16:
17: import cz.cvut.kbss.jopa.model.QueryImpl;
18: import cz.cvut.kbss.jopa.model.TypedQueryImpl;
19: import cz.cvut.kbss.jopa.model.query.Query;
20: import cz.cvut.kbss.jopa.model.query.TypedQuery;
21: import cz.cvut.kbss.jopa.query.QueryParser;
22: import cz.cvut.kbss.jopa.sessions.ConnectionWrapper;
23: import cz.cvut.kbss.jopa.sessions.QueryFactory;
24: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
25: import cz.cvut.kbss.jopa.utils.ErrorUtils;
26:
27: import java.util.Objects;
28:
29: public class SparqlQueryFactory implements QueryFactory {
30:
31: private final UnitOfWorkImpl uow;
32: private final ConnectionWrapper connection;
33:
34: private final QueryParser queryParser;
35:
36: public SparqlQueryFactory(UnitOfWorkImpl uow, ConnectionWrapper connection) {
37: assert uow != null;
38: assert connection != null;
39: this.uow = uow;
40: this.connection = connection;
41: this.queryParser = new SparqlQueryParser();
42: }
43:
44: @Override
45: public Query createNativeQuery(String sparql) {
46: Objects.requireNonNull(sparql, ErrorUtils.constructNPXMessage("sparql"));
47:
48: final QueryImpl q = new QueryImpl(queryParser.parseQuery(sparql), connection);
49: q.setUseBackupOntology(uow.useBackupOntologyForQueryProcessing());
50: return q;
51: }
52:
53: @Override
54: public <T> TypedQuery<T> createNativeQuery(String sparql, Class<T> resultClass) {
55: Objects.requireNonNull(sparql, ErrorUtils.constructNPXMessage("sparql"));
56: Objects.requireNonNull(resultClass, ErrorUtils.constructNPXMessage("resultClass"));
57:
58: final TypedQueryImpl<T> tq = new TypedQueryImpl<>(queryParser.parseQuery(sparql), resultClass, connection, uow);
59: tq.setUnitOfWork(uow);
60: tq.setUseBackupOntology(uow.useBackupOntologyForQueryProcessing());
61: return tq;
62: }
63:
64: @Override
65: public Query createQuery(String query) {
66: Objects.requireNonNull(query, ErrorUtils.constructNPXMessage("query"));
67:
68: // We do not support any more abstract syntax, yet
69: return createNativeQuery(query);
70: }
71:
72: @Override
73: public <T> TypedQuery<T> createQuery(String query, Class<T> resultClass) {
74: Objects.requireNonNull(query, ErrorUtils.constructNPXMessage("query"));
75: Objects.requireNonNull(resultClass, ErrorUtils.constructNPXMessage("resultClass"));
76:
77: // We do not support any more abstract syntax, yet
78: return createNativeQuery(query, resultClass);
79: }
80:
81: @Override
82: public Query createNamedQuery(String name) {
83: final String query = uow.getNamedQueryManager().getQuery(name);
84: return createNativeQuery(query);
85: }
86:
87: @Override
88: public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
89: final String query = uow.getNamedQueryManager().getQuery(name);
90: return createNativeQuery(query, resultClass);
91: }
92: }